import 'package:flutter_test/flutter_test.dart';
import 'package:sms_listener/sms_listener.dart';
import 'package:flutter/services.dart';

void main() {
  const MethodChannel channel = MethodChannel('sms_listener');

  TestWidgetsFlutterBinding.ensureInitialized();

  setUp(() {
    channel.setMockMethodCallHandler((MethodCall methodCall) async {
      switch (methodCall.method) {
        case 'startListening':
          return true;
        case 'stopListening':
          return true;
        case 'isListening':
          return false;
        case 'hasSmsPermissions':
          return true;
        case 'requestSmsPermissions':
          return true;
        default:
          return null;
      }
    });
  });

  tearDown(() {
    channel.setMockMethodCallHandler(null);
  });

  group('SmsListener Tests', () {
    test('应该能创建SmsListener实例', () {
      final smsListener = SmsListener();
      expect(smsListener, isNotNull);
      expect(smsListener.isListening, isFalse);
    });

    test('应该能请求权限', () async {
      final smsListener = SmsListener();
      final hasPermissions = await smsListener.requestPermissions();
      expect(hasPermissions, isTrue);
    });

    test('应该能检查权限状态', () async {
      final smsListener = SmsListener();
      final hasPermissions = await smsListener.hasPermissions();
      expect(hasPermissions, isTrue);
    });
  });

  group('SmsMessage Tests', () {
    test('应该能从Map创建SmsMessage', () {
      final map = {
        'body': '测试短信内容',
        'address': '10086',
        'date': 1640995200000, // 2022-01-01 00:00:00
        'type': 1,
      };

      final message = SmsMessage.fromMap(map);

      expect(message.body, equals('测试短信内容'));
      expect(message.address, equals('10086'));
      expect(message.type, equals(1));
      expect(message.isReceived, isTrue);
      expect(message.isSent, isFalse);
    });

    test('应该能转换为Map', () {
      final message = SmsMessage(
        body: '测试短信',
        address: '10086',
        date: DateTime.fromMillisecondsSinceEpoch(1640995200000),
        type: 1,
      );

      final map = message.toMap();

      expect(map['body'], equals('测试短信'));
      expect(map['address'], equals('10086'));
      expect(map['date'], equals(1640995200000));
      expect(map['type'], equals(1));
    });

    test('应该正确比较SmsMessage对象', () {
      final message1 = SmsMessage(
        body: '测试',
        address: '10086',
        date: DateTime.fromMillisecondsSinceEpoch(1640995200000),
        type: 1,
      );

      final message2 = SmsMessage(
        body: '测试',
        address: '10086',
        date: DateTime.fromMillisecondsSinceEpoch(1640995200000),
        type: 1,
      );

      final message3 = SmsMessage(
        body: '不同内容',
        address: '10086',
        date: DateTime.fromMillisecondsSinceEpoch(1640995200000),
        type: 1,
      );

      expect(message1, equals(message2));
      expect(message1, isNot(equals(message3)));
    });
  });

  group('SmsPermissions Tests', () {
    const MethodChannel permissionChannel =
        MethodChannel('sms_listener/permissions');

    setUp(() {
      permissionChannel.setMockMethodCallHandler((MethodCall methodCall) async {
        switch (methodCall.method) {
          case 'hasSmsPermissions':
            return true;
          case 'requestSmsPermissions':
            return true;
          case 'hasPermission':
            return true;
          case 'getPermissionStatus':
            return 0; // granted
          default:
            return null;
        }
      });
    });

    tearDown(() {
      permissionChannel.setMockMethodCallHandler(null);
    });

    test('应该能检查SMS权限', () async {
      final hasPermissions = await SmsPermissions.hasSmsPermissions();
      expect(hasPermissions, isTrue);
    });

    test('应该能请求SMS权限', () async {
      final granted = await SmsPermissions.requestSmsPermissions();
      expect(granted, isTrue);
    });

    test('应该能获取权限状态', () async {
      final status = await SmsPermissions.getPermissionStatus(
          'android.permission.RECEIVE_SMS');
      expect(status, equals(PermissionStatus.granted));
    });

    test('应该返回必需权限列表', () {
      final permissions = SmsPermissions.requiredPermissions;
      expect(permissions.length, equals(2));
      expect(permissions.contains('android.permission.RECEIVE_SMS'), isTrue);
      expect(permissions.contains('android.permission.READ_SMS'), isTrue);
    });
  });
}
